/*     */ package com.cr.framework.aspectj;
/*     */ 
/*     */ import com.alibaba.fastjson.JSONObject;
/*     */ import com.alibaba.fastjson.serializer.SerializeFilter;
/*     */ import com.alibaba.fastjson.support.spring.PropertyPreFilters;
/*     */ import com.cr.common.annotation.Log;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.enums.BusinessStatus;
/*     */ import com.cr.common.utils.ServletUtils;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.framework.manager.AsyncManager;
/*     */ import com.cr.framework.manager.factory.AsyncFactory;
/*     */ import com.cr.system.domain.SysOperLog;
/*     */ import java.lang.reflect.Method;
/*     */ import java.util.Collection;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import org.aspectj.lang.JoinPoint;
/*     */ import org.aspectj.lang.Signature;
/*     */ import org.aspectj.lang.annotation.AfterReturning;
/*     */ import org.aspectj.lang.annotation.AfterThrowing;
/*     */ import org.aspectj.lang.annotation.Aspect;
/*     */ import org.aspectj.lang.annotation.Pointcut;
/*     */ import org.aspectj.lang.reflect.MethodSignature;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.stereotype.Component;
/*     */ import org.springframework.web.multipart.MultipartFile;
/*     */ 
/*     */ @Aspect
/*     */ @Component
/*     */ public class LogAspect {
/*  42 */   private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
/*     */   
/*  45 */   public static final String[] EXCLUDE_PROPERTIES = new String[] { "password", "oldPassword", "newPassword", "confirmPassword" };
/*     */   
/*     */   @Pointcut("@annotation(com.cr.common.annotation.Log)")
/*     */   public void logPointCut() {}
/*     */   
/*     */   @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
/*     */   public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
/*  61 */     handleLog(joinPoint, null, jsonResult);
/*     */   }
/*     */   
/*     */   @AfterThrowing(value = "logPointCut()", throwing = "e")
/*     */   public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
/*  73 */     handleLog(joinPoint, e, null);
/*     */   }
/*     */   
/*     */   protected void handleLog(JoinPoint joinPoint, Exception e, Object jsonResult) {
/*     */     try {
/*  81 */       Log controllerLog = getAnnotationLog(joinPoint);
/*  82 */       if (controllerLog == null)
/*     */         return; 
/*  88 */       SysUser currentUser = ShiroUtils.getSysUser();
/*  91 */       SysOperLog operLog = new SysOperLog();
/*  92 */       operLog.setStatus(Integer.valueOf(BusinessStatus.SUCCESS.ordinal()));
/*  94 */       String ip = ShiroUtils.getIp();
/*  95 */       operLog.setOperIp(ip);
/*  96 */       operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
/*  97 */       if (currentUser != null) {
/*  99 */         operLog.setOperName(currentUser.getLoginName());
/* 100 */         if (MyStringUtils.isNotNull(currentUser.getDept()) &&
/* 101 */           MyStringUtils.isNotEmpty(currentUser.getDept().getDeptName()))
/* 103 */           operLog.setDeptName(currentUser.getDept().getDeptName()); 
/*     */       } 
/* 107 */       if (e != null) {
/* 109 */         operLog.setStatus(Integer.valueOf(BusinessStatus.FAIL.ordinal()));
/* 110 */         operLog.setErrorMsg(MyStringUtils.substring(e.getMessage(), 0, 2000));
/*     */       } 
/* 113 */       String className = joinPoint.getTarget().getClass().getName();
/* 114 */       String methodName = joinPoint.getSignature().getName();
/* 115 */       operLog.setMethod(className + "." + methodName + "()");
/* 117 */       operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
/* 119 */       getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
/* 121 */       AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
/* 123 */     } catch (Exception exp) {
/* 126 */       log.error("==前置通知异常==");
/* 127 */       log.error("异常信息:{}", exp.getMessage());
/* 128 */       exp.printStackTrace();
/*     */     } 
/*     */   }
/*     */   
/*     */   public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLog operLog, Object jsonResult) throws Exception {
/* 142 */     operLog.setBusinessType(Integer.valueOf(log.businessType().ordinal()));
/* 144 */     operLog.setTitle(log.title());
/* 146 */     operLog.setOperatorType(Integer.valueOf(log.operatorType().ordinal()));
/* 148 */     if (log.isSaveRequestData())
/* 151 */       setRequestValue(joinPoint, operLog); 
/* 154 */     if (log.isSaveResponseData() && MyStringUtils.isNotNull(jsonResult))
/* 156 */       operLog.setJsonResult(MyStringUtils.substring(JSONObject.toJSONString(jsonResult), 0, 2000));
/*     */   }
/*     */   
/*     */   private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception {
/* 168 */     Map<String, String[]> map = ServletUtils.getRequest().getParameterMap();
/* 169 */     if (MyStringUtils.isNotEmpty(map)) {
/* 171 */       String params = JSONObject.toJSONString(map, (SerializeFilter)excludePropertyPreFilter(), new com.alibaba.fastjson.serializer.SerializerFeature[0]);
/* 172 */       operLog.setOperParam(MyStringUtils.substring(params, 0, 2000));
/*     */     } else {
/* 176 */       Object args = joinPoint.getArgs();
/* 177 */       if (MyStringUtils.isNotNull(args)) {
/* 179 */         String params = argsArrayToString(joinPoint.getArgs());
/* 180 */         operLog.setOperParam(MyStringUtils.substring(params, 0, 2000));
/*     */       } 
/*     */     } 
/*     */   }
/*     */   
/*     */   private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
/* 190 */     Signature signature = joinPoint.getSignature();
/* 191 */     MethodSignature methodSignature = (MethodSignature)signature;
/* 192 */     Method method = methodSignature.getMethod();
/* 194 */     if (method != null)
/* 196 */       return method.<Log>getAnnotation(Log.class); 
/* 198 */     return null;
/*     */   }
/*     */   
/*     */   public PropertyPreFilters.MySimplePropertyPreFilter excludePropertyPreFilter() {
/* 206 */     return (new PropertyPreFilters()).addFilter().addExcludes(EXCLUDE_PROPERTIES);
/*     */   }
/*     */   
/*     */   private String argsArrayToString(Object[] paramsArray) {
/* 214 */     String params = "";
/* 215 */     if (paramsArray != null && paramsArray.length > 0)
/* 217 */       for (int i = 0; i < paramsArray.length; i++) {
/* 219 */         if (MyStringUtils.isNotNull(paramsArray[i]) && !isFilterObject(paramsArray[i])) {
/* 221 */           Object jsonObj = JSONObject.toJSONString(paramsArray[i], (SerializeFilter)excludePropertyPreFilter(), new com.alibaba.fastjson.serializer.SerializerFeature[0]);
/* 222 */           params = params + jsonObj.toString() + " ";
/*     */         } 
/*     */       }  
/* 226 */     return params.trim();
/*     */   }
/*     */   
/*     */   public boolean isFilterObject(Object o) {
/* 238 */     Class<?> clazz = o.getClass();
/* 239 */     if (clazz.isArray())
/* 241 */       return clazz.getComponentType().isAssignableFrom(MultipartFile.class); 
/* 243 */     if (Collection.class.isAssignableFrom(clazz)) {
/* 245 */       Collection collection = (Collection)o;
/* 246 */       Iterator iter = collection.iterator();
/* 246 */       if (iter.hasNext())
/* 248 */         return iter.next() instanceof MultipartFile; 
/* 251 */     } else if (Map.class.isAssignableFrom(clazz)) {
/* 253 */       Map map = (Map)o;
/* 254 */       Iterator<Map.Entry> iter = map.entrySet().iterator();
/* 254 */       if (iter.hasNext()) {
/* 256 */         Map.Entry entry = iter.next();
/* 257 */         return entry.getValue() instanceof MultipartFile;
/*     */       } 
/*     */     } 
/* 260 */     return (o instanceof MultipartFile || o instanceof javax.servlet.http.HttpServletRequest || o instanceof javax.servlet.http.HttpServletResponse || o instanceof org.springframework.validation.BindingResult);
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\aspectj\LogAspect.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */